using UnityEngine;

namespace AudioStudio
{
    /// <summary>
    /// ��Ч��������Ч��ʵʱ����3Dλ������
    /// </summary>
    [AddComponentMenu("AudioStudio/EffectSound")]
    public class EffectSound : AudioEventHandler
    {
        public SoundParam[] enables;
        public AudioEventPC[] disables;
        public bool PlayerControl = false;      ///决定event后面是否追加_PC后缀

        new void Awake()
        {
            this.StopOnDestroy = false;
            base.Awake();
        }

        public override void HandleEnableEvent()
        {
            if(AkSoundEngine.IsInitialized())
            {
                PostEnableEvents();
            }
        }

        public override void HandleDisableEvent()
        {
            if(AkSoundEngine.IsInitialized())
            {
                StopEnableEvents();
                PostEvents(disables);
            }
        }

        void PostEvents(AudioEventPC[] events)
        {
            if (AkSoundEngine.IsInitialized() && events != null)
            {
                for (int i = 0; i < events.Length; i++)
                {
                    events[i].Post(this.Player,this.PlayerControl);
                }
            }
        }

        void PostEnableEvents()
        {
            if(AkSoundEngine.IsInitialized())
            {
                for(int i = 0; i < enables.Length; i++)
                {
                    enables[i].audioEvent.Post(this.Player,this.PlayerControl);
                }
            }
        }

        void StopEnableEvents()
        {
            if(AkSoundEngine.IsInitialized())
            {
                for(int i = 0; i < enables.Length; i++)
                {
                    if(enables[i].stop)
                    {
                        enables[i].audioEvent.Stop(this.Player, (int)enables[i].transitionDuration * 1000);
                    }
                }
            }
        }
    }
}
